const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    uploadText: '上传相册',
    canUpload: true,
    videos: [],
    videoIndex: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  // 点击cover播放，其它视频结束
  videoPlay: function (e) {
    var _index = e.currentTarget.id;
    this.setData({
      _index: _index
    })
    //停止正在播放的视频
    var videoContextPrev = wx.createVideoContext(this.data._index)
    videoContextPrev.stop();
    setTimeout(function () {
      //将点击视频进行播放
      var videoContext = wx.createVideoContext(_index)
      videoContext.play();
    }, 500)
  },
  onShow:function(){
    this.setData({
      videoIndex:''
    });
    this.refleshVideos();
  },
  refleshVideos: function() {
    const that = this;
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    const baseUrl = app.globalData.baseUrl;
    wx.request({
      url: url + '/coach/videoList/' + openId,
      success: function(res) {
        if (res.data.success === true) {
          const videos = res.data.content;
          const videoList = [];
          videos.forEach(function(item) {
            item.videoUrl = baseUrl + '/file/' + item.videoFileName;
            item.picUrl = baseUrl + '/file/' + item.picName;
            videoList.push(item);
          });
          that.setData({
            videos: videoList
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  addVideo: function() {
    wx.navigateTo({
      url: '/pages/addVideo/addVideo',
    })
  },
  deleteVideo: function(e) {
    const that = this;
    const index = e.currentTarget.dataset.index;
    const videos = this.data.videos;
    const selectVideo = videos[index];
    const url = app.globalData.url;
    wx.showModal({
      title: '提示',
      content: '确定删除此视频？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: url + '/coach/deleteVideo/' + selectVideo.id,
            success: function(response) {
              if (response.data.success) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                });
                that.refleshVideos();
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'none'
                });
              }
            }
          })
        }
      }
    })
  },
  toEnd: function(e) {
    this.setData({
      videoIndex:''
    });
  }
})